package com.itcast.demo_one

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.itcast.demo_one.bean.LoginRequest
import com.itcast.demo_one.utils.RetrofitClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class NetActivity : AppCompatActivity() {
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_net)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 1. 获取频道列表
        findViewById<Button>(R.id.getChannel).setOnClickListener {
            try {
                // 1.1 开启协程, 获取频道列表
                lifecycleScope.launch(Dispatchers.Main) {
                    // 1.2 切换到I/O线程发起网络请求
                    val res =  withContext(Dispatchers.IO) {
                       RetrofitClient.apiService.getChannels()
                    }

                    // 1.3 回到主线程进行数据处理和UI更新
                    if(res.code == 10000){
                       val channels = res.data?.channels
                        findViewById<TextView>(R.id.channel).text = "获取频道列表成功, 频道名称: ${channels?.joinToString(", ") { it.name }}"
                    } else {
                        Toast.makeText(this@NetActivity, "请求失败", Toast.LENGTH_SHORT).show()
                    }
                }


            } catch (e: Exception){
                Toast.makeText(this, e.message, Toast.LENGTH_SHORT).show()
            }
        }

        // 2. 登录
        findViewById<Button>(R.id.login).setOnClickListener {
            try {
                val loginRequest = LoginRequest(
                    username = "admin",
                    password = "123456"
                )
                lifecycleScope.launch(Dispatchers.Main) {
                    val res = withContext(Dispatchers.IO){
                        RetrofitClient.apiService.loginApi(loginRequest)
                    }

                    if (res.code == 10000){
                        val token = res.data?.token
                        findViewById<TextView>(R.id.tv).text = "登录成功, Token: $token"
                    } else {
                        // 处理响应
                        Toast.makeText(
                            this@NetActivity,
                            "登录失败",
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }


            } catch (e: Exception){
                Toast.makeText(this, e.message, Toast.LENGTH_SHORT).show()
            }
        }
    }
}